package com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.entity.SamPoiData;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.queryParams.SamPoiDataQuaParams;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.service.SamPoiDataService;
import com.css.fxfzxczh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/samPoiData")
@Slf4j
public class SamPoiDataController {
    @Resource
    SamPoiDataService samPoiDataService;
    /**
     * 分页列表
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(SamPoiDataQuaParams queParams, HttpServletRequest request){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=samPoiDataService.getPage(queParams,curPage,pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查询所有数据
     * @param queParams
     * @return
     */
    @GetMapping("/getAllData")
    public RestResponse getAllData(SamPoiDataQuaParams queParams){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            List<SamPoiData> list =samPoiDataService.getAllData(queParams);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 保存
     * @param samPoiData
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody SamPoiData samPoiData){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            samPoiDataService.save(samPoiData);
            jsonObject.put("message","保存成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 导出
     * @param response
     */
    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response,@RequestBody SamPoiDataQuaParams queryParams){
        try {
            samPoiDataService.exportExcel(response,queryParams);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
        }
    }

    /**
     * 导入excel
     * @param file
     * @return
     */
    @PostMapping("/importExcel")
    public RestResponse uploadSampling(@RequestParam("file") MultipartFile file,String xsemId,String xitmId) {
        RestResponse response = null;
        try{
            Map<String, Object> map = samPoiDataService.importExcel(file,xsemId,xitmId);
            boolean res = ((Boolean) map.get("res")).booleanValue();;
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "上传失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 根据当前地震事件id获取所有调查点
     * @param xsemId
     * @return
     */
    @GetMapping("/getSamPoiByXsemId")
    public RestResponse getSamPoiByXsemId(HttpServletRequest request,String xsemId){
        RestResponse restResponse=null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=samPoiDataService.getSamPoiByXsemId(xsemId,curPage,pageSize);
            restResponse = RestResponse.succeed(page);
            restResponse.setMessage("查询成功!");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 抽样点编号
     * @param eventCode
     * @return
     */
    @GetMapping("/getSamPoiCode")
    public RestResponse getSamPoiCode(String eventCode){
        RestResponse response = null;

        try{
            String str=samPoiDataService.getSamPoiCode(eventCode);
            response = RestResponse.succeed(str);
        }catch (Exception e){
            String errorMessage = "生成失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id) {
        RestResponse response = null;
        try {
            SamPoiData samPoiData=samPoiDataService.findById(id);
            response = RestResponse.succeed(samPoiData);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取所有抽样点
     * @param
     * @return
     */
    @GetMapping("/getSamPoiList")
    public RestResponse getSamPoiList(String xsemId){
        RestResponse response = null;
        try{
            List<SamPoiData> samPoiData=samPoiDataService.getSamPoiList(xsemId);
            response = RestResponse.succeed(samPoiData);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 修改
     *
     * @param samPoiData
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody SamPoiData samPoiData) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            samPoiDataService.update(samPoiData);
            jsonObject.put("message", "修改成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "修改失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }


    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse deleteTask(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            samPoiDataService.delete(ids);
            jsonObject.put("message","删除成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查询未关联子区的抽样点
     * @param xsemId
     * @return
     */
    @GetMapping("/getUnassociatedData")
    public RestResponse getUnassociatedData(String xsemId){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            List<SamPoiData> list =samPoiDataService.getUnassociatedData(xsemId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
